package com.gionee.navil.lockscreen;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.ActivityManagerNative;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.Toast;

public class GnLockScreenActivity extends Activity {
	private static final int SENSOR_SHAKE = 1;
	private static final String TAG = "GnLockScreenActivity";

	private SensorManager mSensorManager;
	private Vibrator mVibrator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.zzzzz_gn_navil_ly_main);
		
		setContentView(new GnNavilLockScreen(this));
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterSensorListener();
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerSensorListener();
	}

	private SensorEventListener sensorEventListener = new SensorEventListener() {

		@Override
		public void onSensorChanged(SensorEvent event) {
			float[] values = event.values;
			float x = values[0];
			float y = values[1];
			float z = values[2];

			int medumValue = 19;
			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				if (Math.abs(x) > medumValue || Math.abs(y) > medumValue
						|| Math.abs(z) > medumValue) {
					if (mSensorManager != null) {
						mSensorManager.unregisterListener(sensorEventListener);
					}
					mVibrator.vibrate(200);

					mHandler.sendEmptyMessageDelayed(SENSOR_SHAKE, 400);

				}
			}
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}
	};

	private void registerSensorListener() {
		if (mSensorManager != null) {
			mSensorManager.registerListener(sensorEventListener,
					mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
					SensorManager.SENSOR_DELAY_GAME);

		}
	}

	private void unregisterSensorListener() {
		if (mSensorManager != null) {
			mSensorManager.unregisterListener(sensorEventListener);
		}
	}

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case SENSOR_SHAKE:
				registerSensorListener();
				Toast.makeText(GnLockScreenActivity.this, "open camera",
						Toast.LENGTH_SHORT).show();
				launchActivity(new Intent(
						MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA));
				break;
			}
		}

	};

	private void launchActivity(Intent intent) {
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP
				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
		try {
			ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity();
		} catch (RemoteException e) {
			GnLog.d(TAG, "can't dismiss keyguard on launch");
		}
		try {
			GnLockScreenActivity.this.startActivity(intent);
			GnLockScreenActivity.this.finish();
		} catch (ActivityNotFoundException e) {
			GnLog.d(TAG,
					"Activity not found for intent + " + intent.getAction());
		}
	}

}
